package com.masterclient.app.businessmodule.settingmodule.bankcard.contract;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.common.config.Config;
import com.masterclient.app.businessmodule.settingmodule.api.SettingHttpService;
import com.masterclient.app.businessmodule.usermodule.api.UserHttpService;
import com.masterclient.app.businessmodule.settingmodule.bankcard.bean.NewBankBean;
import com.masterclient.app.businessmodule.settingmodule.bankcard.bean.NewBankCardBean;
import com.masterclient.app.businessmodule.settingmodule.bankcard.bean.OperateResultBean;
import com.masterclient.app.businessmodule.usermodule.login.beans.CaptchaBean;
import com.eoner.baselibrary.utils.EncryptUtil;
import com.masterclient.app.utils.CaptchaUtil;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;


public class BankCardManagePresenter extends BankCardManageContract.Presenter{

    @Override
    public void getBank() {
        mRxManager.add(toSubscribe(SettingHttpService.getInstance().getBank(), new Consumer<CommonBaseResponse<NewBankBean>>() {
            @Override
            public void accept(CommonBaseResponse<NewBankBean> bean) throws Exception {
                if (0 == bean.getCode()){
                    mView.getBankSuccess(bean.getData());
                }else {
                    mView.getBankFailed(bean.getMsg());
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                o.printStackTrace();
                mView.getBankFailed(o.getMessage());
            }
        }));
    }

    @Override
    public void bankManage(String real_name, String id_number, String card_number, String bank_code, String bank_tel, String type,String captcha) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("bankcard_certification_id",id_number);
            jsonObject.put("bankcard_number",card_number);
            jsonObject.put("bank_code",bank_code);
            jsonObject.put("bank_mobile",bank_tel);
            jsonObject.put("captcha",captcha);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(SettingHttpService.getInstance().cardManage(body), (Consumer<OperateResultBean>) bean -> {
            if ("0".equals(bean.getCode())){
                mView.BankManageSuccess();
            }else {
                mView.BankManageFailed(bean.getMsg());
            }
        }, (Consumer<Throwable>) o -> {
            o.printStackTrace();
            mView.BankManageFailed("网络异常，请重试");
        }));
    }

    @Override
    public void bankRemove(String id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("bankcard_certification_id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(SettingHttpService.getInstance().cardRemove(body), (Consumer<OperateResultBean>) bean -> {
            if ("0".equals(bean.getCode())){
                mView.BankRemoveSuccess();
            }else {
                mView.BankRemoveFailed(bean.getMsg());
            }
        }, (Consumer<Throwable>) o -> {
            o.printStackTrace();
            mView.BankRemoveFailed("网络异常，请重试");
        }));
    }

    @Override
    public void getSettingBankCardInfo() {
        mView.baseShowPageLoading(false);
        mRxManager.add(toSubscribe(SettingHttpService.getInstance().getBankCard(), (Consumer<CommonBaseResponse<NewBankCardBean>>) bean -> {
            mView.baseHiddenPageLoading();
            mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
            if (0 == bean.getCode()){
                mView.showBankCard(bean.getData());
            } else {
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                mView.getBankCardFail(bean.getMsg());
            }
        }, (Consumer<Throwable>) o -> {
            o.printStackTrace();
            mView.baseHiddenPageLoading();
            mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
            ToastUtil.showToast(WEB_FAILED_STR);
            mView.getBankCardFail(WEB_FAILED_STR);
        }));
    }

    @Override
    public void getCaptcha(String mobile, String type, String ne) {
        String captchaValidate = "";
        if (CaptchaUtil.CAPTCHA_NUMBER_TIMES >= Config.CAPTCHA_MAX_TIMES){
            Map<String,String> map = new HashMap<>();
            map.put("failureNum","3");
            map.put("clientTime",System.currentTimeMillis()/1000+"");
            captchaValidate = EncryptUtil.encryptRSA(new Gson().toJson(map),Config.captchaKey);
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mobile",mobile);
            jsonObject.put("type",type);
            jsonObject.put("NECaptchaValidate",ne);
            jsonObject.put("captchaValidate",captchaValidate);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<CaptchaBean> observable = UserHttpService.getInstance().getCaptcha(body);
        Disposable disposable = toSubscribe(observable, new Consumer<CaptchaBean>() {
            @Override
            public void accept(@NonNull CaptchaBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.getCaptchaSuccess();
                }else {
                    mView.getCaptchaFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }
}